%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Project SNB Portfolio%%
%
% Strategies we implement in the paper
%
% Ptf.exclusion = 2; Ptf.screen = 0; Ptf.Intensity_Footprint = 1 + 2 (Global exclusion and reinvestment)
% Ptf.exclusion = 2; Ptf.screen = 1; Ptf.Intensity_Footprint = 1 + 2 (Sectoral exclusion and reinvestment)
% Ptf.exclusion = 3; Ptf.screen = 0; Ptf.Intensity_Footprint = 1 + 2 (Global exclusion and sectoral reinvestment)
% Ptf.exclusion = 4; Ptf.screen = 0; Ptf.Intensity_Footprint = 1 + 2 (Global exclusion and primary industry reinvestment)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clc;
clearvars;
close all;

Ptf.exclusion = 2; %   1 = worst firms are excluded,  
                   %   2 = worst firms are excluded and reinvested in best firms, 
                   %   3 = overall exclusion, sectoral reinvestment

Ptf.screen = 0; % 0: global screening, 
                % 1: screening by sector 

Ptf.Quantiles = 0.01;

Year=2019;

% 
% filename =  ['Codes/RCFS/IdxKeep/SNB/MergedSNBTC_' num2str(Year)];
% load(filename,'MergedSNBTC');
%        
% MergedSNBTC.Shares=[];
% MergedSNBTC.Region=[];
% MergedSNBTC.Scope1=[];
% MergedSNBTC.Scope13=[];
% MergedSNBTC.Intensity1=[];
% MergedSNBTC.Intensity13=[];
% MergedSNBTC.MktCap=[];
% MergedSNBTC.TrucostSectorName=[];
% 
% indexUS = strcmp(MergedSNBTC.Country,'United States of America');
% MergedSNBTC = MergedSNBTC(indexUS,:);
% 
% Data2019=MergedSNBTC;
% filename =  ['Data2019'];
% save('Data2019','Data2019');
% save('ListSectors','ListSectors');
% 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Portfolio analysis
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load('Data2019','Data2019');
   
Data=Data2019;
ListSectors=unique(Data.Sector);

Data.WeightsRescaled = Data.Weights / sum(Data.Weights);
Data.MktCap = Data.MarketValue./(Data.x_OfCSO/100) * 1000000; 

keepScore = ~isnan(Data.Scope12);
NewWeights2 = Data.WeightsRescaled(keepScore) / sum(Data.WeightsRescaled(keepScore));

tmp0=Data.Scope12(keepScore);
tmp1=Data.x_OfCSO(keepScore);
idx = ~isnan(tmp0.*tmp1);
Bench.NbFirms = sum(idx);
Bench.Emissions = sum(tmp0(idx).*tmp1(idx))/100;

tmp=Data.Intensity12(keepScore);idx = ~isnan(tmp);
Bench.WAIntensity = sum(NewWeights2(idx) .* tmp(idx)) / sum(NewWeights2(idx));
  
tmp1=Data.Scope12(keepScore).*Data.x_OfCSO(keepScore)/100;
tmp2=Data.MarketValue(keepScore)/1;idx = ~isnan(tmp1.*tmp2);
Bench.Footprint = sum(tmp1(idx)) / sum(tmp2(idx));

tmp1=Data.Scope12(keepScore) .* Data.x_OfCSO(keepScore);
tmp2=Data.Revenues(keepScore) .* Data.x_OfCSO(keepScore);idx = ~isnan(tmp1.*tmp2);
Bench.Intensity = sum(tmp1(idx)) / sum(tmp2(idx))/1;
        
tmp=Data.TRIp1(keepScore)./Data.TRI(keepScore)-1;
Bench.AnnualReturn = sum(NewWeights2(idx) .* tmp(idx)) / sum(NewWeights2(idx));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('------------------------------------------------------------------')   
disp('Global exclusion and reinvestment')
disp('------------------------------------------------------------------')   

Ptf.exclusion = 2;Ptf.screen = 0;

Ptf1 = CriterionRebalancing(Data,Ptf,ListSectors);

%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('------------------------------------------------------------------')   
disp('Sectoral exclusion and reinvestment')
disp('------------------------------------------------------------------')   

Ptf.exclusion = 2;Ptf.screen = 1;

Ptf2 = CriterionRebalancing(Data,Ptf,ListSectors);

%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('------------------------------------------------------------------')   
disp('Global exclusion and sectoral reinvestment')
disp('------------------------------------------------------------------')   


Ptf.exclusion = 3;Ptf.screen = 0;

Ptf3 = CriterionOverallSectoral(Data,Ptf,ListSectors);

%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('------------------------------------------------------------------')   
disp('Global exclusion and primary industry reinvestment')
disp('------------------------------------------------------------------')   

Ptf.exclusion = 4;Ptf.screen = 0;

Ptf4 = CriterionOverallSubSectoral(Data,Ptf,ListSectors);
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

disp('------------------------------------------------------------------')   
disp('Summary of results')
disp('------------------------------------------------------------------')   

disp('Benchmark')
disp(Bench)
disp('Global exclusion and reinvestment')
disp(Ptf1)
disp('Sectoral exclusion and reinvestment')
disp(Ptf2)
disp('Global exclusion and sectoral reinvestment')
disp(Ptf3)
disp('Global exclusion and primary industry reinvestment')
disp(Ptf4)

return

